
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.String;

import Algo.Algo;
/*	this algorithm return tags if its an ascii file*/
public class AsciiAlgo implements Algo{
	private boolean checkIfASCIINIO(Path p) throws IOException
    {
		return ((Files.probeContentType(p)).compareToIgnoreCase("text/plain")==0);
    }
	public String [] tagFile(Path child) {
		
		String pathString=child.toString();
		File myFile = new File(pathString);
		if 	(myFile.isDirectory()){
			return null;
		}
		
		String tags[] =null;	
		try{
				if (checkIfASCIINIO(child)){
					String searchText[] = {"java","bar-ilan","daniel","yeala","work"}; 
					String fileName = child.toString();
					StringBuilder sb =new StringBuilder();
					try {
						BufferedInputStream bIn =new BufferedInputStream(new FileInputStream(fileName));
						int pos = 0; 
						int avl = bIn.available(); 
						while ( avl != 0 ) {
							byte[] buffer = new byte[avl];
							bIn.read(buffer, pos, avl); 
							pos += avl; 
							String strTemp =new String(buffer);
							sb.append(strTemp);
							avl = bIn.available(); 
						}
					}catch(IOException ex) {
						ex.printStackTrace();
					}
					String fileText = sb.toString();
					String temp="";
					boolean flag=false;
					for (int i=0; i<searchText.length;i++){
						if (fileText.indexOf(searchText[i])>=0){
							if (flag){
								temp=temp+".";
							}
							temp=temp+""+searchText[i];
							flag=true;
						}
					}
					if (flag){
						tags=temp.split("\\.");
					}
				}
		}
		catch (IOException xx){
			System.out.println("AsciiAlgo: error");
			return null;
		}
		return tags;
	}
	public String descript(){
		return "this algorithm return tags if its an ascii file";
	}
	public String toString(){
		return "AsciiAlgo";
	}
}
